using Emergen.Framework.Logger;

using System.Diagnostics;

namespace AxureRPVersionSelector
{
    internal static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                InitConfigFilePath();
#if DEBUG
                Debugger.Launch();
#endif
                if (args.Length <= 0)
                {
                    throw new ArgumentNullException(nameof(args));
                }

                
                var cmd = args[0];
                Logs.Instance.Info($"cmdline=[{string.Join(" ", args)}]");

                switch (cmd)
                {
                    case "--setup":
                    case "--config":
                        {
                            OpenConfigForm();
                            break;
                        }
                    default:
                        {
                            var filename = cmd;
                            AxureRPVersionSelector.Instance.OpenFile(filename);
                            break;
                        }
                }
            }
            catch (Exception ex)
            {
                Logs.Instance.Error(ex);
            }
        }

        private static void InitConfigFilePath()
        {
            try
            {
                if(!ConfigManager.IsLoad())
                {
                    var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "app.config.json");
                    ConfigManager.ConfigFilePath = path;
                }
            }
            catch(Exception ex)
            {
                Logs.Instance.Error(ex);
            }
        }

        private static void OpenConfigForm()
        {
            ApplicationConfiguration.Initialize();
            Application.Run(new frmMain());
        }

    }
}